package com.apes.fn.scm.merchant.disbursement.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.fico.debitnote.model.BusinessDebitNote;
import com.apes.fn.scm.merchant.account.model.MerchantAccountSettlement;
import com.apes.fn.scm.merchant.account.model.MerchantBankAccount;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.tuple.date.DateTime;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.finance.model.BankAccount;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * @Title: DisbursementItem
 * @Description: 赋能商户结算排款单明细
 * @author: cyy
 * @create: 2023/2/08 11:10
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = DisbursementItem.CONST_TABLE_NAME)
public class DisbursementItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "fn_merchant_disbursement_item";

    @Id
    @Column(name = "id",nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "赋能商户结算排款单明细", padding = 10)
    @Comment("编码")
    private String id;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "disbursement_id", referencedColumnName = "id")
    @Comment("赋能商户结算排款单编码")
    private Disbursement disbursement;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "merchant_account_settlement_id", referencedColumnName = "id")
    @Comment("对账单2.0")
    private MerchantAccountSettlement merchantAccountSettlement;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "business_debit_note_id", referencedColumnName = "id")
    @Comment("预支单")
    private BusinessDebitNote businessDebitNote;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "merchant_id", referencedColumnName = "id")
    @Comment("商户编码")
    private PartyRole merchant;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("部门编码")
    private Dept dept;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "reconcile_begin_date")
    @Comment("结算期起")
    private Date reconcileBeginDate;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "reconcile_end_date")
    @Comment("结算期止")
    private Date reconcileEndDate;

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fn_tag", referencedColumnName = "id")
    @Comment("赋能标签")
    private Enumeration fnTag;

    @Column(name = "settlement_state", length = 36, nullable = false)
    @States(name = "状态", values = {
            @State(id = "sent", name = "新建"),
            @State(id = "pending", name = "待排款"),
            @State(id = "done", name = "已完成"),
            @State(id = "confirmation", name = "商户确认"),
            @State(id = "financialAudit", name = "财务审计"),
            @State(id = "arrangement", name = "排款中"),
            @State(id = "pay", name = "部分付款"),
            @State(id = "payment", name = "已付款")})
    private String settlementState;

    @Column(name = "financial_audit_date")
    @Comment("财务发出日期")
    private Date financialAuditDate;

    @Column(name = "merchant_confirmation_date")
    @Comment("门店确认日期")
    private Date merchantConfirmationDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "merchant_bank_account_id", referencedColumnName = "id")
    @Comment("对账单银行账户")
    private MerchantBankAccount merchantBankAccount;

    @Comment("银行账户")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "bank_account_id", referencedColumnName = "id")
    private BankAccount bankAccount;

    @Column(name = "payable_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("应付款金额")
    private double payableAmount = 0;

    @Column(name = "disbursement_already_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("已排款金额")
    private double disbursementAlreadyAmount = 0;

    @Column(name = "disbursement_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("本次排款金额")
    private double disbursementAmount = 0;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "pre_amount_date")
    @Comment("预付款日期")
    private Date preAmountDate;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "amount_date")
    @Comment("付款日期")
    private Date amountDate;

    @Column(name = "already_pay_amount", columnDefinition = ApesConst.FIELD_16_4)
    @Comment("已付款金额")
    private Double alreadyPayAmount = 0.0D;

    @Column(name = "pay_state", columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("付款状态")
    private boolean payState = false;

    @Column(name = "order_id", length = 16)
    @Comment("单据号码")
    private String orderId;

    @Column(name = "business_type", length = 120)
    @Comment("排款类型")
    private String businessType; // merchantAccount:对账单 businessDebitNote:预支单

    @Name
    @Column(name = "remark", length = 120)
    @Comment("摘要")
    private String remark;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reconcile_id", referencedColumnName = "id")
    @Comment("对账人")
    private PartyRole reconcileId;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @DateTime(GenerationTime.INSERT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "business_date", updatable = false)
    @Comment("业务日期")
    private Date businessDate;
}
